﻿using System;
using System.Windows.Forms;

namespace IBNet.UtilsDataStructuresAndExtensions
{
   public static class TextBoxExtensions
   {
      /// <summary>
      /// Asynchronously writes a message then a line break to this TextBox.
      /// </summary>
      /// <param name="message">A message to write to this text box.</param>
      public static void WriteLine(this TextBox textBox, string message)
      {
         textBox.InvokeIfNeeded(delegate
         {
            textBox.AppendText(message);
            textBox.AppendText("\r\n");
         }, InvocationMethod.BeginInvoke);
      }

      /// <summary>
      /// Asynchronously writes a series of messages then a line break to this TextBox.
      /// Use this overload to avoid string concatenation costs.
      /// </summary>
      /// <param name="messages">A multi-string message to write to this text box.</param>
      public static void WriteLine(this TextBox textBox, params string[] messages)
      {
         textBox.InvokeIfNeeded(delegate
         {
            foreach (string message in messages)
            {
               textBox.AppendText(message);
            }
            textBox.AppendText("\r\n");
         }, InvocationMethod.BeginInvoke);
      }
   }
}
